﻿using Essensoft.AspNetCore.Payment.WeChatPay;
using JindaTech.Common.Configuration;
using JindaTech.Common.Const;
using JindaTech.Common.NLog;
using Microsoft.AspNetCore.Hosting; 
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NLog;
using sdk_cop;
using System;
using System.IO;
using System.Net.Http;

namespace JindaTech.Api.Controllers.Icbc
{
    public class IcbcTool
    {
        IcbcParam icbcParam = null;
        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly IOptions<WeChatPayOptions> _optionsAccessor;
        private string PrivateKey = "";
        private string PublicKey = "";
        public IcbcTool(IWebHostEnvironment hostingEnvironment, IOptions<WeChatPayOptions> optionsAccessor)
        {
            _hostingEnvironment = hostingEnvironment;
            _optionsAccessor = optionsAccessor;

            icbcParam = AppSetting.IcbcParam;
            string webRootPath = _hostingEnvironment.WebRootPath;
            PrivateKey = File.ReadAllText(webRootPath + "/yourname.pri");
            PublicKey = File.ReadAllText(webRootPath + "/yourname.pub");
            //PrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCia+J4gpUOELpVgdi4NM7spdj8Kcp/i2IJ+A4y/fTiQFmpOz2axsm1fGRMkY3eCmynpRmLRF5il9fpOnrxM/aVU5XrM6YsX0fxg5MTmffwaFtALFeh2wIyBASEsEIMHq6M4sU3Fp5TRiJAu1CAQwTHimDXG4+MVHJYDnS+C71yrHOku9sBz9FaUHVH6+Uf2voAtuMOnjUewRGKGJtf9D00IdvHxlz5jB1ylrdhpmGaf/t2YFhC7dKx5PpsPoZ3DIvWoJmb+ZcvMk0K1AQuI0BGwq2rTlSF1DSautrxvaxo/Rfs7eFXCMl6beoynE2o81PU8avqzacH+l4M0MKWeYk7AgMBAAECggEAAZ0Gu+G4L1KVN0I57+J+Z7ViCWc03r14tNak9Xa2VwUCnv66pu3wlP/uUyBjA2ChIa0vKsX8FgiiGWv+SqUUfvk8lQCHVwt/QHPpIxm1b8rplSKboDz3sZ1+mf7SOSKY40dhZ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= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMpjaWjngB4E3ATh+G1DVAmQnIpiPEFAEDqRfNGAVvvH35yDetqewKi0l7OEceTMN1C6NPym3zStvSoQayjYV+eIcZERkx31KhtFu9clZKgRTyPjdKMIth/wBtPKjL/5+PYalLdomM4ONthrPgnkN4x4R0+D4+EBpXo8gNiAFsNwIDAQAB";
        }

        public string Showpay(string notifyUrl, string outTradeNo, string orderAmt, string openId, string body)
        {

            if (string.IsNullOrEmpty(body) == false)
            {
                if (body.Length > 128) body = body.Substring(0, 128);
            }
            string html = "";


            //UiIcbcClient client = new UiIcbcClient(icbcParam.AppId, IcbcConstants.SIGN_TYPE_RSA2, PrivateKey, null, null, IcbcConstants.CHARSET_UTF8);
            UiIcbcClient client = new UiIcbcClient(icbcParam.AppId,  PrivateKey, IcbcConstants.CHARSET_UTF8);
            var ServiceUrl = "https://gw.open.icbc.com.cn/ui/cardbusiness/aggregatepay/b2c/online/ui/consumepurchaseshowpay/V1";
            IcbcRequest icbcRequest = new IcbcRequest();
            icbcRequest.setServiceUrl(ServiceUrl);
            icbcRequest.setNotifyUrl(notifyUrl);

            IcbcRequest.IcbcRequestBiz icbcRequestBiz = new IcbcRequest.IcbcRequestBiz();
            icbcRequest.setBizContent(icbcRequestBiz);

            icbcRequestBiz.setMerId(icbcParam.MerId);
            icbcRequestBiz.setOutTradeNo(outTradeNo);
            icbcRequestBiz.setMerPrtclNo(icbcParam.MerPrtclNo);
            icbcRequestBiz.setOrderAmt(orderAmt);
            icbcRequestBiz.setNotifyUrl(notifyUrl);
            icbcRequestBiz.setIcbcAppId(icbcParam.AppId);
            icbcRequestBiz.setOpenId(openId);
            icbcRequestBiz.setSaledepname("中山市工人文化宫");
            icbcRequestBiz.setBody(body);
            //icbcRequestBiz.setSubject("");
            //icbcRequestBiz.setNotifyType("HS");
            icbcRequestBiz.setNotifyType("AG");
            icbcRequestBiz.setResultType("0");
            //icbcRequestBiz.setMerAcct("");
            icbcRequestBiz.setExpireTime("120");
            //icbcRequestBiz.setAttach("");
            //icbcRequestBiz.setReturnUrl("");
            //icbcRequestBiz.setPayLimit("");
            icbcRequestBiz.setShopAppId(_optionsAccessor.Value.AppId);
            //icbcRequestBiz.setOrderApdInf("");
            //icbcRequestBiz.setDetail("");
            //icbcRequestBiz.setCustName("");
            //icbcRequestBiz.setCustCertType("");
            //icbcRequestBiz.setCustCertNo("");
            //icbcRequestBiz.setGoodsTag("");

            //TODO
            string Postparam  = JsonConvert.SerializeObject(icbcRequestBiz);
             
            NLogUtil.WriteDBLog(LogLevel.Info, Postparam);

            html = client.buildPostForm(icbcRequest);


            return html;
        }

        public IcbcOrderQryResponse orderqry(string orderno)
        {
            string ServiceUrl = "https://gw.open.icbc.com.cn/api/cardbusiness/aggregatepay/b2c/online/orderqry/V1";
            DefaultIcbcClient client = new DefaultIcbcClient(icbcParam.AppId, IcbcConstants.SIGN_TYPE_RSA2, PrivateKey, PublicKey);
            IcbcOrderQryRequest icbcOrderQryRequest = new IcbcOrderQryRequest();
            icbcOrderQryRequest.setServiceUrl(ServiceUrl);

            IcbcOrderQryRequest.IcbcOrderQryRequestBiz icbcOrderQryRequestBiz = new IcbcOrderQryRequest.IcbcOrderQryRequestBiz();

            icbcOrderQryRequest.setBizContent(icbcOrderQryRequestBiz);
            icbcOrderQryRequestBiz.setMerId(icbcParam.MerId);
            icbcOrderQryRequestBiz.setOutTradeNo(orderno);
            icbcOrderQryRequestBiz.setDealFlag("0");
            icbcOrderQryRequestBiz.setIcbcAppid(icbcParam.AppId);
            icbcOrderQryRequestBiz.setMerPrtclNo(icbcParam.MerPrtclNo);
            //IcbcResponse response = client.execute(icbcOrderQryRequest, DateTime.Now.Ticks.ToString());
            IcbcOrderQryResponse icbcResponse = (IcbcOrderQryResponse)client.execute(icbcOrderQryRequest, DateTime.Now.Ticks.ToString());
            return icbcResponse;
        }
    }
}
